package com.fenghongzhang.day010matrix;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class MyMatrix extends View {

    private Matrix matrix;
    private Paint paint;
    private Bitmap bitmap;
    public MyMatrix(Context context) {
        super(context);
    }

    public MyMatrix(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        matrix = new Matrix();
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmap,matrix,paint);
    }

    public void setTran(float x, float y){
//        matrix.setTranslate(x,y);
//        invalidate();
//        matrix.preTranslate(x,y);
        matrix.postTranslate(x,y);
    }
    public void setScale(float x,float y){
//        matrix.setScale(x,y);
//        invalidate();
//        matrix.preScale(x,y);
        matrix.postScale(x,y);
    }
    public void setRotate(float d){
        matrix.setRotate(d);
//        invalidate();
    }

    public void setKew(float x,float y){
        matrix.setSkew(x,y);
    }

    public void shui(){
        Bitmap map = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(map);
        canvas.drawBitmap(bitmap,0,0,paint);
        paint.setTextSize(20);
        canvas.drawText("www.baidu.com",0,20,paint);
        bitmap = map;
        invalidate();
    }
}
